﻿using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.Planlama
{
    public partial class xrDosyaKapama : DevExpress.XtraReports.UI.XtraReport
    {
        DataSet ds;
        List<string[]> Captions = new List<string[]>();
        int LanguageIndex = 0;
        string[] CurrentCaptions
        {
            get
            {
                return Captions[LanguageIndex];
            }
        }

        public xrDosyaKapama()
        {
            InitializeComponent();
        }

        public xrDosyaKapama(string planlamaId)
            : this()
        {
            ds = Session.DataManager.ExecuteDataSet("GetDosyaKapamaRaporu", planlamaId);

            DataRelation FK_DosyaKapama = new DataRelation("FK_DosyaKapama",
              ds.Tables["Table"].Columns["DosyaNo"],
              ds.Tables["Table1"].Columns["DosyaNo"]);
            ds.Relations.Add(FK_DosyaKapama);

            BindDstoReport();
        }

        public xrDosyaKapama(string idx, int customer, int languageIndex)
            : this()
        {
            ds = Session.DataManager.ExecuteDataSet("GetDosyaKapamaRaporuFromOrders", idx, customer);

            DataRelation FK_DosyaKapama = new DataRelation("FK_DosyaKapama",
              ds.Tables["Table"].Columns["DosyaNo"],
              ds.Tables["Table1"].Columns["DosyaNo"]);
            ds.Relations.Add(FK_DosyaKapama);

            switch (languageIndex)
            {
                case 3: LanguageIndex = 1; break;   //EN
                default: LanguageIndex = 0; break;   //TR
            }            

            Captions.Add("Dosya Kapama Raporu;Atölye;Müşteri;Dosya No;Model Adı;Sipariş Adedi;Kesim Adedi;Yükleme Adedi;Tarih;Ay;Kumaş Artikeli;Kesim Adedi;Müşteriye Giden 1.Kalite;2.Kalite Dokuma Hatası;3.Kalite Defolu;Numune;İptal;Toplamlar;Planlama Sorumlusu :;Üretim Sorumlusu :;Fason Görevlisi :;Kalan 1.Kalite;2.Kalite Üretim Hatası".Split(';'));
            Captions.Add("File Closing Report;Factory;Customer;File;Model Name;Order Pieces;Cut Pieces;Ship Pieces;Date;Month;Article;Cut Pieces;1st Quality Sended Customer;2nd Quality;Flaw;Sample;Cancel;General Summary;Planning Executive;Production Executive;Subcontractor Officer;Remain 1.Quality;2.Quality Production Error".Split(';'));

            for (int i = 0; i < CurrentCaptions.Length; i++)
            {
                string caption = CurrentCaptions[i];
                ((XRTableCell)this.FindControl("x" + (i + 1).ToString(), false)).Text = caption;
            }
            BindDstoReport();
        }

        void BindDstoReport()
        {
            //DataRow dr = ds.Tables["Table"].Rows[0];

            //tcDate.Text = DateTime.Now.ToShortDateString();
            //tcAtelye.Text = dr["Atolye"].ToString();
            //tcMusteri.Text = dr["Musteri"].ToString();
            //tcDosyaNo.Text = dr["DosyaNo"].ToString();
            //tcModel.Text = dr["Model"].ToString();
            //tcSiparisAdedi.Text = dr["SiparisAdedi"].ToString();

            this.DataMember = "Table";
            this.DataSource = ds;

            tcAtelye.DataBindings.Add("Text", null, "Table.Atolye");
            tcMusteri.DataBindings.Add("Text", null, "Table.Musteri");
            tcDosyaNo.DataBindings.Add("Text", null, "Table.DosyaNo");
            tcModel.DataBindings.Add("Text", null, "Table.Model");
            tcSiparisAdedi.DataBindings.Add("Text", null, "Table.SiparisAdedi");
            xrBarCode1.DataBindings.Add("Text", null, "Table.PlanlamaId");

            this.DetailReport.DataMember = "Table.FK_DosyaKapama";
            this.DetailReport.DataSource = ds;

            tcArtikel.DataBindings.Add("Text", null, "Table.FK_DosyaKapama.Artikel");
        }

        private DataTable RemoveUnusedBDCols(DataTable dt)
        {
            string colNames = "";


            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string activeColName = dt.Columns[i].ColumnName;


                if (activeColName.StartsWith("x"))
                {
                    activeColName = "[" + dt.Columns[i].ColumnName + "]";
                    if (dt.Select("ISNULL(" + activeColName + ",'')<>''").Length == 0)
                    {
                        colNames += dt.Columns[i].ColumnName + ";";
                    }
                }
                else
                    colNames += dt.Columns[i].ColumnName + ";";
            }

            foreach (string colName in colNames.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                dt.Columns.Remove(colName);
            }

            return dt;

        }

        private void AddBDColumns(string colName, float ColWidth)
        {
            string caption;
            if (colName != "Total")
            {
                string rownr = GetCurrentColumnValue("RowNr").ToString();
                DataRow dr = ds.Tables["Table"].Select("RowNr=" + rownr)[0];
                caption = dr[colName].ToString();
            }

            else
            {
                caption = LanguageIndex == 0 ? "Toplam" : "Total";
            }

            XRTableCell cell = new XRTableCell();
            cell.Name = "header" + colName;
            cell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
            cell.Font = new System.Drawing.Font("Calibri", 9.75F, System.Drawing.FontStyle.Bold);
            cell.Text = caption;//colName.Substring(1, colName.Length - 1);
            cell.WidthF = ColWidth;

            XRTableCell detailCell = new XRTableCell();
            detailCell.Name = "detail" + colName;
            detailCell.Text = "";
            detailCell.WidthF = ColWidth;

            XRTableCell footerCell = new XRTableCell();
            footerCell.Name = "footer" + colName;
            footerCell.Text = "";
            footerCell.WidthF = ColWidth;


            //cell.Text = "TOTAL";

            cell.Borders = ((DevExpress.XtraPrinting.BorderSide)((((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top)
                | DevExpress.XtraPrinting.BorderSide.Right)
                | DevExpress.XtraPrinting.BorderSide.Bottom)));

            detailCell.Borders =
            footerCell.Borders =
                ((DevExpress.XtraPrinting.BorderSide)(((DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right)
                | DevExpress.XtraPrinting.BorderSide.Bottom)));


            trHeader.Cells.Add(cell);
            trDetail.Cells.Add(detailCell);
            trFooter.Cells.Add(footerCell);
        }

        private void DetailReport_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            string rownr = GetCurrentColumnValue("RowNr").ToString();
            DataTable dt = ds.Tables["Table"].Select("RowNr=" + rownr).CopyToDataTable();

            xrTable1.BeginInit();
            xrTable2.BeginInit();
            xrTable4.BeginInit();
            DataTable dtBeden = RemoveUnusedBDCols(dt);
            
            int columncount = dtBeden.Columns.Count == 0 ? 1 : dtBeden.Columns.Count;
            float colWidth = 0;
            if (columncount < 15)
                colWidth = 905 / (6 + columncount);
            else
                colWidth = 625 / columncount;

            string cellNames = "";
            for (int i = 0; i < trHeader.Cells.Count; i++) //foreach (XRTableCell tc in trHeader)
            {
                string cellName = trHeader.Cells[i].Name;
                if (trHeader.Cells[i].Name.Contains("header"))
                    cellNames += cellName.Replace("header", "") + ";";
            }

            foreach (string colName in cellNames.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                trHeader.Cells["header" + colName].Dispose();
                trDetail.Cells["detail" + colName].Dispose();
                trFooter.Cells["footer" + colName].Dispose();
            }

            for (int i = 0; i < dtBeden.Columns.Count; i++)
            {
                string colName = dtBeden.Columns[i].ColumnName;
                AddBDColumns(colName, colWidth);
            }

            AddBDColumns("Total", colWidth);

            xrTable1.WidthF = xrTable2.WidthF = xrTable4.WidthF = 1123;
            x11.WidthF = tcArtikel.WidthF = x18.WidthF = 174;
            xrTable1.EndInit();
            xrTable2.EndInit();
            xrTable4.EndInit();
        }

        private void trHeader_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {

        }        
    }
}
